package com.igeek.car.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.car.constant.MessageConstant;
import com.igeek.car.entity.Announcement;
import com.igeek.car.result.Result;
import com.igeek.car.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.igeek.car.vo.PageQueryVO;
import org.springframework.web.bind.annotation.*;

/**
 * AnnouncementController 公告接口，仅仅只做接口暴露
 * AnnouncementService    公告业务
 *      AnnouncementServiceImpl  公告业务的具体实现
 * AnnouncementMapper     公告的持久层交互
 *
 * Swagger测试地址：http://localhost:8080/swagger-ui.html
 *
 * @author Aries04r
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/admin/announcement")
@Api(tags = "公告的接口管理")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @ApiOperation("分页条件查询公告")
    @GetMapping("findPage")
    public Result findPage(PageQueryVO vo){
        Page<Announcement> page = announcementService.findPage(vo);
        if(page != null){
            return new Result(true, MessageConstant.QUERY_ANNOUNCEMENT_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_ANNOUNCEMENT_FAIL);
    }

    @ApiOperation("新增公告")
    @PostMapping("add")
    public Result add(@RequestBody Announcement announcement){
        boolean flag = announcementService.save(announcement);
        if(flag){
            return new Result(true, MessageConstant.ADD_ANNOUNCEMENT_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_ANNOUNCEMENT_FAIL);
    }

    @ApiOperation("通过ID查询公告信息")
    @GetMapping("{id}")
    public Result findOne(@PathVariable("id") Integer id){
        Announcement announcement = announcementService.getById(id);
        if(announcement != null){
            return new Result(true, MessageConstant.QUERY_ANNOUNCEMENT_SUCCESS, announcement);
        }
        return new Result(false, MessageConstant.QUERY_ANNOUNCEMENT_FAIL);
    }

    @ApiOperation("编辑公告")
    @PutMapping("update/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody Announcement announcement) {

        // 调用服务层更新公告
        boolean flag = announcementService.updateById(announcement);
        if (flag) {
            return new Result(true, MessageConstant.UPDATE_ANNOUNCEMENT_SUCCESS);
        } else {
            return new Result(false, MessageConstant.UPDATE_ANNOUNCEMENT_FAIL);
        }
    }

    @ApiOperation("删除公告")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean flag = announcementService.del(id);
        if(flag){
            return new Result(true, MessageConstant.DELETE_ANNOUNCEMENT_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_ANNOUNCEMENT_FAIL);
    }
}